package hsit.design;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.excel.write.metadata.fill.FillWrapper;

import java.io.File;
import java.util.*;

/**
 * 生成类说明
 *
 * @author huanggy
 * @date 2023/02/20
 **/
public class GenerateClassDesc {


    public static void main(String[] args) throws Exception {

        // 设置要生成的数据内容，主要操作不同系统的差别
        List<ClassInfo> infoList = getData("D:\\codespace\\hsrcspace\\tobacco-mp-cc\\tobacco-mp-cc-main\\tobacco-mp-cc-client\\src\\main\\java\\com\\tobacco\\mp\\cc\\client\\api",
                "Spu");

        // excel模板
        String templateFileName = "D:\\codespace\\toolspace\\huanggy_tools\\huanggy_tools\\src\\main\\java\\hsit\\design\\ClassDesc.xlsx";

        // 生成文件存放目录
        String targetPath = "D:\\codespace\\toolspace\\huanggy_tools\\huanggy_tools\\src\\main\\java\\hsit\\design\\";
        try {
            for (int i = 0; i < infoList.size(); i++) {
                ClassInfo classInfo = infoList.get(i);
                String fileName = targetPath + classInfo.getClassName() + ".xlsx";
                ExcelWriter excelWriter = EasyExcel.write(fileName).withTemplate(templateFileName).build();
                WriteSheet writeSheet = EasyExcel.writerSheet().build();
                excelWriter.fill(new FillWrapper("field", classInfo.getFields()), writeSheet);
                excelWriter.fill(new FillWrapper("method", classInfo.getMethods()), writeSheet);
                Map<String, Object> map = new HashMap<>();
                map.put("className", classInfo.getClassName());
                map.put("classDesc", classInfo.getClassDesc());
                map.put("classPath", classInfo.getClassPath());
                map.put("superClassName", classInfo.getSuperClassName());
                excelWriter.fill(map, writeSheet);
                excelWriter.finish();
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    private static List<ClassInfo> getData(String javaFilePath, String filterKey) {
        List<ClassInfo> data = new LinkedList<>();
        File file = new File(javaFilePath);
        for (File listFile : file.listFiles()) {
            if (listFile.isDirectory()) {
                continue;
            }
            if (filterKey != null && !listFile.toString().contains(filterKey)) {
                continue;
            }
            ClassInfo classInfo = new Doclet(listFile.toString()).exec();
            data.add(classInfo);
        }
        return data;
    }

}
